package com.youfan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.youfan.entity.YoufanOrder;
import com.youfan.mapper.OrderMapper;
import com.youfan.page.CommonPage;
import com.youfan.service.YoufanOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class YoufanOrderServiceImpl extends ServiceImpl<OrderMapper, YoufanOrder> implements YoufanOrderService {

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public int insertOne(YoufanOrder youfanorder) {
        youfanorder.setCreateTime(new Date());
        youfanorder.setPayType(-1);
        youfanorder.setStatus(0);
        return orderMapper.insert(youfanorder);
    }

    @Override
    public CommonPage<YoufanOrder> queryByCondtion(YoufanOrder youfanorder) {
        PageHelper.startPage(youfanorder.getPageNum(),youfanorder.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();

        if(youfanorder.getUserId() != null){
            queryWrapper.eq("user_id", youfanorder.getUserId());
        }

        if(youfanorder.getProductId() != null){
            queryWrapper.eq("product_id", youfanorder.getUserId());
        }
        if(youfanorder.getSourceType() != null){
            queryWrapper.eq("source_type", youfanorder.getSourceType());
        }
        if(youfanorder.getPayType() != null){
            queryWrapper.eq("pay_type", youfanorder.getPayType());
        }
        if(youfanorder.getStatus() != null){
            queryWrapper.eq("status", youfanorder.getStatus());
        }
        if(youfanorder.getOrderType() != null){
            queryWrapper.eq("order_type", youfanorder.getOrderType());
        }
        if(StringUtils.isNotBlank(youfanorder.getOrderSn())){
            queryWrapper.like("order_sn", youfanorder.getOrderSn());
        }


        List<YoufanOrder> list = orderMapper.selectList(queryWrapper);


        return CommonPage.restPage(list);
    }

    @Override
    public YoufanOrder findByid(YoufanOrder youfanOrder) {
        return orderMapper.selectById(youfanOrder.getId());
    }

    @Override
    public int updateOne(YoufanOrder youfanOrder) {
        youfanOrder.setUpdateTime(new Date());
        return orderMapper.updateById(youfanOrder);
    }
}
